class_name Shell extends Area2D

@export var shooter : Node2D
@export var damage := 1.0
@export var speed := 1000.0
@export var max_distance := 700.0
@onready var collision_shape_2d: CollisionShape2D = $CollisionShape2D



var start_pos : Vector2

func _ready() -> void:
	start_pos = global_position


func _physics_process(delta: float) -> void:
	var dir := Vector2.from_angle(rotation)
	global_position += dir * speed * delta
	#最大射程销毁
	if start_pos.distance_to(global_position) >= max_distance: die(false)



func die(explode: bool = true) -> void:
	if explode: Global.add_animation("normal_explode", global_position, Vector2(0.5, 0.5), rotation, 1.0, Color(1, 0.4, 0.24, 1.0))
	queue_free()


func _on_timer_timeout() -> void:
	die()



func _on_body_shape_entered(body_rid: RID, body: Node2D, _body_shape_index: int, _local_shape_index: int) -> void:
	set_deferred("monitoring", false)
	if body is TileMap and "body_entered" in body: 
		body.body_entered(body_rid, body, self)
	elif "hurt" in body:
		body.hurt(self)
	die()




func _on_area_entered(_area: Area2D) -> void:
	die()
